<template>
  <div class="tab-bar-item" @click="itemClick">
    <!--    在插槽外面再定义一个空div,防止插槽替换时，属性得不到保留-->
    <div v-if="isActive">
      <slot name="item-icon-active"></slot>
    </div>
    <div v-else>
      <slot name="item-icon"></slot>
    </div>

    <!--    如果把class绑定在slot插槽上，当插槽位置被整体替换时，插槽上绑定的属性也就被一起替换了。
            故：在外层包一个空div，给该div绑定class，然后样式继承
    -->
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {}
    },
    props: {
      path: String,
      activeColor: {
        type: String,
        default: "blue"
      }
    },
    methods: {
      itemClick() {
        this.$router.replace(this.path);

      }
    },
    computed: {
      isActive() {
        //当前活跃的path为 =》 this.$route.path
        return this.$route.path.includes(this.path)
      },
      activeStyle() {
        return this.isActive ? {color: this.activeColor} : {};
      }
    }
  };
</script>

<style>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;

    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .tab-bar-item img {
    width: 24px;
    height: 24px;
    margin-top: 3px;
    margin-bottom: 2px;

  }

</style>
